﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using BLL;

public partial class kmPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //绑定数据源
            BindData();
        }
        //验证列表视图是否有数据
        viewIsNoData();
    }

    //绑定数据源
    private void BindData()
    {
        gvKM.DataSource = km_BLL.GetSource();
        gvKM.DataBind();
    }

    //取消
    protected void btnCancle_Click(object sender, EventArgs e)
    {
        btnRefur.Text = "添加";
        txtKMName.Text = "";
        txtDate.Text = "";
        chkZF.Checked = false;
    }

    //添加或更新
    protected void btnRefur_Click(object sender, EventArgs e)
    {
        //判断科目名称和日期是否为空
        if (txtKMName.Text.Length == 0 || txtDate.Text.Length == 0)
        {
            ShowMessage("必要信息未填写！");
            return;
        }
        switch (btnRefur.Text)
        {
            case "添加":
                //添加科目
                bool bl = km_BLL.Add(txtKMName.Text, txtDate.Text, chkZF.Checked);
                if (bl)
                {
                    ShowMessage("添加成功！");
                    //刷新当前页
                    Response.Redirect("kmPage.aspx");
                }
                else
                    ShowMessage("添加失败！");
                break;
            case "更新":
                //更新科目
                bool b = km_BLL.Update(updateIndex, txtKMName.Text, txtDate.Text, chkZF.Checked);
                if (b)
                {
                    ShowMessage("更新成功！");
                    //刷新当前页
                    Response.Redirect("kmPage.aspx");
                }
                else
                    ShowMessage("更新失败！");
                btnRefur.Text = "添加";
                break;
        }
    }

    //选择
    public static string updateIndex = "";
    protected void gvKM_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //当前选择项的识别号
        updateIndex = gvKM.DataKeys[e.NewSelectedIndex][0].ToString();
        //获取当前选中行
        GridViewRow row = gvKM.Rows[e.NewSelectedIndex];
        txtKMName.Text = row.Cells[1].Text;
        txtDate.Text = row.Cells[2].Text;
        chkZF.Checked = row.Cells[3].Text=="是"?true:false;
        btnRefur.Text = "更新";
    }

    //翻页
    protected void gvKM_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvKM.PageIndex = e.NewPageIndex;
        BindData();
    }

    //删除
    protected void gvKM_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //删除数据项
        km_BLL.Delete(gvKM.DataKeys[e.RowIndex][0].ToString());
        //重新绑定数据
        BindData();
    }

    #region 脚本
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvKM.Rows.Count == 0)
        {
            RegisterStartupScript("prompt", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未添加相关数据)'</script>");
        }
    }
    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        //注册脚本显示消息
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }
    #endregion

}
